package com.store.app.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.gson.Gson;
import com.store.app.R;
import com.store.app.adapter.GridViewAdapter;
import com.store.app.bean.GoodsBean;
import com.store.app.imlife.utils.ToastTools;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/1.
 */
public class GoodsFragment extends Fragment implements IOnHttpListener{

    private GridView gridView;              //商品展示
    private TextView tvNotGoods;            //没有商品提示
    private View v;
    private GridViewAdapter gridViewAdapter;
    private List<GoodsBean> goodsBeanList = new ArrayList<GoodsBean>();  //商品列表展示数据
    private ProgressBar pb;     //进度条
    private NetRequest netRequest;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    if(goodsBeanList.size()<=0){
                        pb.setVisibility(View.GONE);
                        tvNotGoods.setVisibility(View.VISIBLE);
                    }else{
                        pb.setVisibility(View.GONE);
                        tvNotGoods.setVisibility(View.GONE);
                    }
                    gridViewAdapter.notifyDataSetChanged();
                    break;
                case 2:
                    pb.setVisibility(View.GONE);
                    tvNotGoods.setVisibility(View.VISIBLE);
                    break;
            }
        }
    };
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
         v = inflater.inflate(R.layout.fragment_goods, container, false);
        initView();
        netRequest = new NetRequest(this,getActivity());
        netRequest.requestGoodsList(1);
        return v;
    }
    public void initView(){
        pb = (ProgressBar) v.findViewById(R.id.pb);
        gridView = (GridView) v.findViewById(R.id.gv_rss);
        tvNotGoods  = (TextView) v.findViewById(R.id.reminder);//没有商品提示
        gridViewAdapter = new GridViewAdapter(getActivity(),goodsBeanList);
        gridView.setAdapter(gridViewAdapter);
        if(goodsBeanList.size()<=0){
            pb.setVisibility(View.VISIBLE);
            tvNotGoods.setVisibility(View.GONE);
        }else{
            pb.setVisibility(View.GONE);
            tvNotGoods.setVisibility(View.GONE);
        }
    }

    @Override
    public void onAfinalFail() {
        ToastTools.showShort(getActivity(),"请检查本地网络!");
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        if(requestId == 1){
            try {
                JSONObject jsonObjectData = new JSONObject(result);
                JSONArray listArray = jsonObjectData.getJSONArray("list");
                List<GoodsBean> goodsBean_list = new ArrayList<>();
                Gson gson = new Gson();
                for (int i = 0; i < listArray.length(); i++) {
                    GoodsBean goodsBean = new GoodsBean();
                    goodsBean = gson.fromJson(listArray.getJSONObject(i).toString(), GoodsBean.class);
                    goodsBean_list.add(goodsBean);
                }
                goodsBeanList.clear();
                if (goodsBean_list != null) {
                    goodsBeanList.addAll(goodsBean_list);
                }
                handler.sendEmptyMessage(1);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
            if(requestId == 1){
                handler.sendEmptyMessage(2);
            }
    }
}
